package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 分页操作
     *
     * @param qo
     * @return
     */
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //土司操作
    public List<Destination> listToast(Long parentId) {
        List<Destination> list = new ArrayList();
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        list.add(dest);

        while (dest.getParent() != null) {
            dest = destinationMapper.selectByPrimaryKey(dest.getParent().getId());
            list.add(dest);
        }
        //集合顺序的逆序
        Collections.reverse(list);
        System.out.println(list.size());
        for (Destination d : list) {
            System.out.println(d.getName());
        }
        return list;
    }

    /**
     * 区域管理编辑和添加(查询所有目的地)
     *
     * @return
     */
    public List<Destination> listDests() {
        return destinationMapper.selectAll();
    }

    /**
     * 区域管理的编辑和添加操作
     *
     * @param entity
     * @return
     */
    public AjaxResult saveOrUpdate(Region entity) {
        AjaxResult result = new AjaxResult();
        try {
            if (entity.getId() == null) {
                regionMapper.insert(entity);
            } else {
                regionMapper.updateByPrimaryKey(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 根据区域id查询所有目的地(关联地区的查询)前端页面热门目的地区域下面的目的地查询
     *
     * @param regionId
     * @return
     */
    public List<Destination> listHotDestByRegionId(Long regionId) {

        return destinationMapper.selectByRegionId(regionId);
    }

    /**
     * 单个目的地的查询操作
     *
     * @param destId
     * @return
     */
    public Destination get(Long destId) {
        return destinationMapper.selectByPrimaryKey(destId);
    }


    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    /**
     * 获取该目的地的国家,通过土司直接获取
     *
     * @param destId
     * @return
     */
    public Destination getCountry(Long destId) {
        List<Destination> list = listToast(destId);
        return list.get(0);
    }

    /**
     * 获取该目的地下的省份,通过土司直接获取
     *
     * @param destId
     * @return
     */
    public Destination getProvince(Long destId) {
        List<Destination> list = listToast(destId);
        return list.size() > 1 ? list.get(1) : null;
    }

    /**
     * 根据区域id查询所有目的地(关联地区的查询)前端页面热门目的地区域下面的目的地查询
     *
     * @param rid
     * @return
     */
    public List<Destination> listDestByRegionId(Long rid) {
        //查询区域id下的关联地区的id
        Long[] regions = regionMapper.selectByPrimaryKey(rid).getRefIds();
        //在mapper中循环遍历
        return destinationMapper.selectByIds(regions);
    }

}
